package edu.ncu.controller;

import edu.ncu.dao.GreatDao;
import edu.ncu.entity.*;
import edu.ncu.service.GreatService;
import edu.ncu.service.PublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

@Controller
public class GreatController {

    @Autowired
    private PublishService publishService ;
    @Autowired
    private GreatService greatService ;

    /**
     * 点赞
     * publish:id:great  记录的是指定动态的点赞个数
     * publish:id:great:user:id 记录的是指定动态的点赞用户是谁
     * @{/publish/{pId}/great(pId=${publish.pId})}
     * @param pId
     * @return
     */
    @GetMapping("/publish/{pId}/great")
    public String greatPublish(@PathVariable int pId , HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user") ;
        Publish publish = publishService.updatePublishGreat(pId);
        if (publish != null) {
            // 新增点赞表的数据
            Great great = new Great() ;
            great.setUserId(user.getUserId());
            great.setGreatObjectType(0);
            great.setGreatObjectId(pId);
            greatService.GreatToPublish(pId,user.getUserId()) ;
        }
        return "/index";
    }



    @GetMapping("/photo/{id}/great")
    public String greatPhoto(@PathVariable int id , HttpServletRequest request, Model model) {
        User user = (User) request.getSession().getAttribute("user") ;
        Photo photo = null ;
        if (photo != null) {
            Great great = new Great() ;
            great.setUserId(user.getUserId());
            great.setGreatObjectType(1);
            great.setGreatObjectId(id);
            greatService.GreatToPhoto(id,user.getUserId()) ;
        }
        return "photos" ;
    }

    /**
     * 第一个id表示publish的id，第二个id表示
     * @param id
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/comment/{id}/great")
    public String greatComment(@PathVariable int id , HttpServletRequest request, Model model) {

        Comment comment = null ;
        User user = (User) request.getSession().getAttribute("user") ;
        if (comment != null) {
            Great great = new Great() ;
            great.setUserId(user.getUserId());
            great.setGreatObjectType(1);
            great.setGreatObjectId(id);
            greatService.GreatToComment(id,user.getUserId()) ;
        }
        return "publish_detail" ;
    }
}
